package cn.relx.basic.listener;

import cn.relx.basic.service.PermissionScanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener //表示自定义监听器，需要被spring管理
@Slf4j //加上此注解就可以在类中主动打印log4j日志
public class PermissionScanInitInstener implements ServletContextListener {
    @Autowired
    private PermissionScanService permissionScanService;

    // Spring容器初始化之后被调用
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        // 为了防止我们的代码比较复杂影响项目启动速度，我们创建线程来运行
        new Thread(new Runnable() {
            @Override
            public void run() {
                log.info("contextInitialized start...");
                // 执行permission数据添加方法
                permissionScanService.scanPermission();
                log.info("contextInitialized stop...");
            }
        }).start();
    }

    // 程序停止时被调用
    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        // 主动打印日志
        log.info("PermissionScanInitInstener stop..");
    }
}
